package entityUpdater

import (
	"errors"
	"fmt"
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedisRpc"
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedisService"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEntity"
	"net/http"
)

type entityUpdater struct {
}

func (e *entityUpdater) UpdateDeviceRespond(operateRespondVO *commRedisRpc.OperateRespondVO, clientName string) (err error) {
	// 定义一个最终的异常拦截，用来防止可能的代码BUG
	defer func() {
		if r := recover(); r != nil {
			err = errors.New(fmt.Sprintf("异常信息：%v", r))
		}
	}()

	if operateRespondVO.Data == nil {
		return nil
	}

	deviceName := operateRespondVO.DeviceName
	deviceType := operateRespondVO.DeviceType
	manufacturer := operateRespondVO.Manufacturer

	// 找到redis中存在的数据
	deviceEntity := edgeEntity.Type.DeviceEntity.Instance()
	deviceEntity.DeviceName = deviceName
	deviceEntity.DeviceType = deviceType
	deviceEntity.Manufacturer = manufacturer
	existEntity, err := commRedisService.GetEntityByServiceKey(edgeEntity.Type.DeviceEntity.Type(), deviceEntity.MakeServiceKey())
	if err != nil {
		return err
	}
	if existEntity == nil {
		return nil
	}

	// 取出基础信息，后面需要用到
	deviceEntity.BaseEntity = existEntity.GetBaseEntity()

	// 检测是否为空
	if operateRespondVO.Data == nil {
		return nil
	}

	// 数据1: 设备通信状态
	commStatus := operateRespondVO.Data["commStatus"]
	err = deviceCommStatusUpdater.updateStatusEntity(deviceEntity.BaseEntity.Id, commStatus)
	if err != nil {
		return err
	}

	// 提取数值
	deviceValues := operateRespondVO.Data["value"]
	if deviceValues == nil {
		return nil
	}

	// 数据2: 设备的状态类型数据
	statusValues := deviceValues.(map[string]interface{})["status"]
	deviceStatusValueUpdater.updateDeviceStatusValue(deviceEntity, statusValues)

	// 数据3: 设备的记录类数据
	recordList := deviceValues.(map[string]interface{})["record"]
	deviceRecordValueUpdater.updateDeviceRecordValue(deviceName, manufacturer, deviceType, recordList)

	// 数据4: 用户的操作记录类数据
	operateRecordValueUpdater.updateOperateRecordValue(clientName, operateRespondVO)

	return nil
}

func (e *entityUpdater) DeleteValueEntity(requestVO *commRedisRpc.RestFulRequestVO) (respond *commRedisRpc.RestFulRespondVO) {
	// 定义一个最终的异常拦截，用来防止可能的代码BUG
	defer func() {
		if r := recover(); r != nil {
			// 异常返回
			respondVO := &commRedisRpc.RestFulRespondVO{}
			respondVO.BindRequestVO(requestVO)
			respondVO.Msg = fmt.Sprintf("更新设备数据，发生异常：%v", r)
			respondVO.Code = http.StatusInternalServerError
			respond = respondVO
		}
	}()

	mapList := requestVO.Data.([]interface{})

	deviceValues := make(map[string][]string)
	for _, data := range mapList {
		mp := data.(map[string]interface{})

		deviceName := mp["deviceName"]
		objectName := mp["objectName"]

		_, ok := deviceValues[deviceName.(string)]
		if !ok {
			deviceValues[deviceName.(string)] = make([]string, 0)
		}

		deviceValues[deviceName.(string)] = append(deviceValues[deviceName.(string)], objectName.(string))
	}

	for deviceName, deviceValue := range deviceValues {
		deviceStatusValueUpdater.deleteValueEntity(deviceName, deviceValue)
	}

	respondVO := &commRedisRpc.RestFulRespondVO{}
	respondVO.BindRequestVO(requestVO)
	respondVO.Code = http.StatusOK

	return respondVO
}
